package com.ykdl.member.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.models.PostActor;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.BitmapHelp;

public class FreakyChatChildItem extends RelativeLayout {
	
	private Context context;
	private TopicBean topic;
	private ImageView iv_child_icon;
	private TextView tv_chat_content;
	private ImageView iv_freaky_icon;
	private TextView tv_freaky_name;
	private TextView tv_chat_like_count;
	private TextView tv_chat_reply_count;
	public FreakyChatChildItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public FreakyChatChildItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public FreakyChatChildItem(Context context, TopicBean topic) {
		super(context);
		this.context = context;
		this.topic = topic;
		init();
	}
	
	public void init(){
		RelativeLayout  view = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.freaky_chat_child_item_view, null);
		iv_child_icon = (ImageView) view.findViewById(R.id.iv_child_icon);
		tv_chat_content = (TextView) view.findViewById(R.id.tv_chat_content);
		iv_freaky_icon = (ImageView) view.findViewById(R.id.iv_freaky_icon);
		tv_freaky_name = (TextView) view.findViewById(R.id.tv_freaky_name);
		tv_chat_like_count = (TextView) view.findViewById(R.id.tv_chat_like_count);
		tv_chat_reply_count = (TextView) view.findViewById(R.id.tv_chat_reply_count);
		
		/**设置适配**/
//		int screenWidth = MobileUtils.getScreenWidthIntPx();
//		int view_item_width = (screenWidth - MobileUtils.dpToPx(36)) / 2;
//		LayoutParams params = (LayoutParams) view.getLayoutParams();
//		params.width = view_item_width;
		
		/**绑定数据**/
		bindData();
		
		addView(view);
//		return view;
	}

	private void bindData() {
		final String child_icon_url = getChildIconUrl();
		if(child_icon_url != null){
			BitmapHelp.getBitmapUtils(context).display(iv_child_icon, getChildIconUrl());
//			iv_child_icon.setUrl(((HomeActivity)(context)).TAG, getChildIconUrl(), ImageOptionsFactory.getBigPortraitOptions(context));
		}else{
			iv_child_icon.setBackgroundResource(R.drawable.iask_main_middle_img);
		}
		
		tv_chat_content.setText(topic.getTitle());
		final String freaky_icon = getFreakyIconUrl();
		if(freaky_icon != null){
			BitmapHelp.getBitmapUtils(context).display(iv_freaky_icon, freaky_icon);
//			iv_freaky_icon.setUrl(((HomeActivity)(context)).TAG, freaky_icon, ImageOptionsFactory.getBigPortraitOptions(context));
		}else{
			iv_child_icon.setBackgroundResource(R.drawable.iask_main_middle_img);
		}
		
		tv_freaky_name.setText(topic.getPost_actor().getDisplay_name());
		tv_chat_like_count.setText(topic.getLikes_count() + "");
		tv_chat_reply_count.setText(topic.getReply_count() + "");
	}
	
	private String getChildIconUrl(){
		if(topic.getPost_actor() != null){
			final PostActor post_actor = topic.getPost_actor();
			if(post_actor.getBaby_profile() != null){
				final FileMetas avatar = post_actor.getBaby_profile().getAvatar();
				if(avatar != null){
					return avatar.getDownload_urls().getBig().getUrl();
				}
			}
		}
		return null;
	}
	private String getFreakyIconUrl(){
		if(topic.getPost_actor() != null){
			if(topic.getPost_actor().getAvatar_file_meta() != null){
				return topic.getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
			}
		}
		
		return null;
	}
}
